package com.zhihuichongcheng.application.bean.task;

import com.smartlib.cmnObject.util.StringUtil;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.util.CollectionUtils;
import com.zhihuichongcheng.application.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * task Created by LK on 2017/1/13.
 */
public class TaskBean {
    public static List<TaskType> taskTypeList = new ArrayList<>();
    public static List<TaskPriority> taskPriorities = new ArrayList<>();

    private String name;
    private String content;
    private String data;
    private String time;
    private String createTime;
    private String finishTime;
    private String receiveUserId;
    private String location;
    private int userId;
    private int priority;
    private int type = 1;
    private int subtaskCount;
    private int subtaskFinish;
    private int userMissionId;
    private int state;

    //执行者
    private ArrayList<UserBean> receiveUsers = new ArrayList<>();
    //参与者
    private ArrayList<UserBean> copyUsers = new ArrayList<>();

    public static void setTaskPriorities(List<TaskPriority> priorityList) {
        taskPriorities.clear();
        taskPriorities.addAll(priorityList);
    }

    public static void setTaskTypeList(List<TaskType> typeList) {
        taskTypeList.clear();
        taskTypeList.addAll(typeList);
    }

    public String getTitle() {
        return name;
    }

    public void setTitle(String title) {
        this.name = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    /**
     * 重新设置执行者
     */
    public void resetExecutors(List<UserBean> executors) {
        this.receiveUsers.clear();
        this.receiveUsers.addAll(executors);
    }

    /**
     * 重新设置抄送人员
     */
    public void resetCCUsers(List<UserBean> userList) {
        this.copyUsers.clear();
        this.copyUsers.addAll(userList);
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getExecutorNicknames() {
        return getUserNames(receiveUsers);
    }

    private String getUserNames(List<UserBean> data) {
        final String[] names = {""};
        CollectionUtils.iterator(data, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean itemUser = (UserBean) data;
                names[0] += itemUser.getUserName() + ",";
                return false;
            }
        });
        if (names[0].length() > 0)
            return names[0].substring(0, names[0].length() - 1);
        return names[0];
    }

    public ArrayList<UserBean> getExecutorUsers() {
        return receiveUsers;
    }

    public String getCCUserNicknames() {
        return getUserNames(copyUsers);
    }

    public String getExecutorUserIds() {
        return getUserIds(receiveUsers);
    }

    private String getUserIds(List<UserBean> data) {
        final String[] ids = {""};
        CollectionUtils.iterator(data, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean itemUser = (UserBean) data;
                ids[0] += itemUser.getUserId() + ",";
                return false;
            }
        });
        if (ids[0].length() > 0)
            return ids[0].substring(0, ids[0].length() - 1);
        return ids[0];
    }

    public String getCCUserUserIds() {
        return getUserIds(copyUsers);
    }

    public ArrayList<UserBean> getCCUser() {
        return copyUsers;
    }

    public String getFinishTimeFormat() {
        String finishTime = getDate() + " " + getTime();
        finishTime = finishTime.replace("年", "-");
        finishTime = finishTime.replace("月", "-");
        finishTime = finishTime.replace("日", "");
        finishTime = finishTime.replace(" : ", ":");
        finishTime = finishTime.replace(" 截止", ":00");
        return finishTime;
    }

    public String getDate() {
        if (StringUtil.isEmpty(data) &&
                !StringUtil.isEmpty(finishTime)) {
            if (finishTime.contains(" ") &&
                    finishTime.contains("-")) {
                String[] date = finishTime.split(" ")[0].split("-");
                String year = date[0];
                String month = date[1];
                String day = date[2];
                data = year + "年" + month + "月" + day + "日";
            } else if (finishTime.contains("年") &&
                    finishTime.contains("月") &&
                    finishTime.contains("日") &&
                    finishTime.contains(" ")) {
                data = finishTime.split(" ")[0];
            }
        }
        return data;
    }

    public String getTime() {
        if (StringUtil.isEmpty(time) &&
                !StringUtil.isEmpty(finishTime) &&
                finishTime.contains(" ") &&
                finishTime.contains(":")) {
            String[] timeSource = finishTime.split(" ")[1].split(":");
            String hour = timeSource[0];
            String minute = timeSource[1];
            String sec = timeSource[2];
            time = hour + " : " + minute + " 截止";
        }

        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public void setDate(String data) {
        this.data = data;
    }

    public String getFinishTime() {
        return finishTime;
    }

    public String getSimpleFinishTime() {
        if (StringUtils.isEmpty(finishTime))
            return "";
        else
            return finishTime.split(" ")[0].replace("年", ".").replace("月", ".").replace("日", "");
    }

    public int getSubTaskFinish() {
        return subtaskFinish;
    }

    public int getSubTaskCount() {
        return subtaskCount;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public String getTypeName() {
        final String[] name = {""};
        CollectionUtils.iterator(taskTypeList, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                TaskType itemType = (TaskType) data;
                if (itemType.getId() == type) {
                    name[0] = itemType.getName();
                    return true;
                }
                return false;
            }
        });
        return name[0];
    }

    public int getPriority() {
        return priority;
    }

    public void setPriority(int priority) {
        this.priority = priority;
    }

    public String getPriorityName() {
        final String[] name = {""};
        CollectionUtils.iterator(taskPriorities, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                TaskPriority itemType = (TaskPriority) data;
                if (itemType.getId() == priority) {
                    name[0] = itemType.getName();
                    return true;
                }
                return false;
            }
        });
        return name[0];
    }

    public int getCurrentPriorityIndex() {
        final int[] index = {-1};
        CollectionUtils.iterator(taskPriorities, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                TaskPriority item = (TaskPriority) data;
                if (item.getId() == priority) {
                    index[0] = position;
                    return true;
                }
                return false;
            }
        });
        return index[0];
    }

    public int getTaskId() {
        return userMissionId;
    }

    public void setTaskId(int userMissionId) {
        this.userMissionId = userMissionId;
    }

    /**
     * 是否打开状态
     */
    public boolean isLive() {
        return state == 1;
    }

    public int getCreateUserId() {
        return userId;
    }

    /**
     * 判断用户是否在执行者当中
     */
    public boolean isUserInReceiveUsers(final int userId) {
        final boolean[] result = {false};
        CollectionUtils.iterator(receiveUsers, new CollectionUtils.AIIteratorListCallBack() {
            @Override
            public boolean callback(Object data, int position) {
                UserBean itemUser = (UserBean) data;
                if (!StringUtil.isEmpty(itemUser.getUserId()) && userId == Integer.parseInt(itemUser.getUserId()))
                    result[0] = true;
                return false;
            }
        });
        return result[0];
    }
}